using System;
using System.Drawing;
using System.Windows.Forms;

namespace WebView2Project
{
    public partial class ScriptResultForm : Form
    {
        public ScriptResultForm()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.lblScriptTitle = new System.Windows.Forms.Label();
            this.txtScript = new System.Windows.Forms.TextBox();
            this.lblResultTitle = new System.Windows.Forms.Label();
            this.txtResult = new System.Windows.Forms.TextBox();
            this.btnClose = new System.Windows.Forms.Button();
            this.btnCopyResult = new System.Windows.Forms.Button();
            this.SuspendLayout();
            
            // 
            // lblScriptTitle
            // 
            this.lblScriptTitle.AutoSize = true;
            this.lblScriptTitle.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F, System.Drawing.FontStyle.Bold);
            this.lblScriptTitle.Location = new System.Drawing.Point(12, 15);
            this.lblScriptTitle.Name = "lblScriptTitle";
            this.lblScriptTitle.Size = new System.Drawing.Size(100, 17);
            this.lblScriptTitle.TabIndex = 0;
            this.lblScriptTitle.Text = "执行的脚本代码：";
            
            // 
            // txtScript
            // 
            this.txtScript.BackColor = System.Drawing.Color.LightGray;
            this.txtScript.Font = new System.Drawing.Font("Consolas", 9F);
            this.txtScript.Location = new System.Drawing.Point(15, 40);
            this.txtScript.Multiline = true;
            this.txtScript.Name = "txtScript";
            this.txtScript.ReadOnly = true;
            this.txtScript.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.txtScript.Size = new System.Drawing.Size(557, 120);
            this.txtScript.TabIndex = 1;
            
            // 
            // lblResultTitle
            // 
            this.lblResultTitle.AutoSize = true;
            this.lblResultTitle.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F, System.Drawing.FontStyle.Bold);
            this.lblResultTitle.Location = new System.Drawing.Point(12, 175);
            this.lblResultTitle.Name = "lblResultTitle";
            this.lblResultTitle.Size = new System.Drawing.Size(68, 17);
            this.lblResultTitle.TabIndex = 2;
            this.lblResultTitle.Text = "执行结果：";
            
            // 
            // txtResult
            // 
            this.txtResult.BackColor = System.Drawing.Color.White;
            this.txtResult.Font = new System.Drawing.Font("Consolas", 9F);
            this.txtResult.Location = new System.Drawing.Point(15, 200);
            this.txtResult.Multiline = true;
            this.txtResult.Name = "txtResult";
            this.txtResult.ReadOnly = true;
            this.txtResult.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.txtResult.Size = new System.Drawing.Size(557, 150);
            this.txtResult.TabIndex = 3;
            
            // 
            // btnCopyResult
            // 
            this.btnCopyResult.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F);
            this.btnCopyResult.Location = new System.Drawing.Point(415, 365);
            this.btnCopyResult.Name = "btnCopyResult";
            this.btnCopyResult.Size = new System.Drawing.Size(75, 30);
            this.btnCopyResult.TabIndex = 4;
            this.btnCopyResult.Text = "复制结果";
            this.btnCopyResult.UseVisualStyleBackColor = true;
            this.btnCopyResult.Click += new System.EventHandler(this.btnCopyResult_Click);
            
            // 
            // btnClose
            // 
            this.btnClose.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F);
            this.btnClose.Location = new System.Drawing.Point(497, 365);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(75, 30);
            this.btnClose.TabIndex = 5;
            this.btnClose.Text = "关闭";
            this.btnClose.UseVisualStyleBackColor = true;
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            
            // 
            // ScriptResultForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(584, 407);
            this.Controls.Add(this.btnClose);
            this.Controls.Add(this.btnCopyResult);
            this.Controls.Add(this.txtResult);
            this.Controls.Add(this.lblResultTitle);
            this.Controls.Add(this.txtScript);
            this.Controls.Add(this.lblScriptTitle);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "ScriptResultForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "JavaScript执行结果";
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        public void SetResult(string script, string result)
        {
            txtScript.Text = script;
            txtResult.Text = FormatResult(result);
        }

        private string FormatResult(string result)
        {
            if (string.IsNullOrEmpty(result))
                return "执行完成，无返回值";
            
            // 移除JSON字符串的外层引号
            if (result.StartsWith("\"") && result.EndsWith("\"") && result.Length > 1)
            {
                try
                {
                    // 简单的JSON字符串处理，移除外层引号
                    result = result.Substring(1, result.Length - 2);
                    // 处理转义字符
                    result = result.Replace("\\\"", "\"").Replace("\\n", "\n").Replace("\\r", "\r").Replace("\\t", "\t");
                }
                catch
                {
                    // 如果处理失败，保持原样
                }
            }
            
            return result;
        }

        private void btnCopyResult_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(txtResult.Text))
                {
                    Clipboard.SetText(txtResult.Text);
                    MessageBox.Show("结果已复制到剪贴板", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"复制失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private System.Windows.Forms.Label lblScriptTitle;
        private System.Windows.Forms.TextBox txtScript;
        private System.Windows.Forms.Label lblResultTitle;
        private System.Windows.Forms.TextBox txtResult;
        private System.Windows.Forms.Button btnClose;
        private System.Windows.Forms.Button btnCopyResult;
    }
} 